package com.cloudfun.campusshare.code.generate.definition;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Huqin on 2020/5/27
 */
@Getter
public class MethodCommentDefinition extends CommentDefinition {

    public static final String INDENTS = JavaFileDefinition.INDENTS + " ";

    private MethodDefinition methodDefinition;

    public MethodCommentDefinition(String comment, MethodDefinition methodDefinition) {
        super(comment);
        this.methodDefinition = methodDefinition;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(JavaFileDefinition.INDENTS).append("/**").append("\n");
        sb.append(INDENTS).append("* ").append(getComment()).append("\n");
        sb.append(INDENTS).append("*").append("\n");
        // 参数
        List<Class> parameters = methodDefinition.getParameters();
        String parametersStr = parameters.stream().map(parameter -> INDENTS + "* @param " + StringUtils.uncapitalize(parameter.getSimpleName()))
                .collect(Collectors.joining("\n"));
        sb.append(parametersStr).append("\n");
        Class retClazz = methodDefinition.getRetClazz();
        if (!retClazz.equals(Void.class)) {
            sb.append(INDENTS).append("* @return").append("\n");
        }
        sb.append(INDENTS).append("*/");
        // 返回值
        return sb.toString();
    }

}
